package leetcode.editor.commons;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wq
 *
 * 基于数组构建二叉树
 * [1,null,2,3]
 */
public class BuidTreeUtils {
        public static TreeNode buildTreeByArr(Integer[] arr,int len,int index){
            if(arr==null||arr.length<=0||index>=len){
                return null;
            }


            TreeNode root=null;
            if(arr[index]!=null){
                root=new TreeNode(arr[index]);
            }

            if(root!=null){
                root.left=buildTreeByArr(arr,len,2*index+1);
                root.right=buildTreeByArr(arr,len,2*index+2);
            }

            return root;

        }

        /*
        * 前序遍历测试
        * */
        public static void preorder(TreeNode root){
            if(root==null){
                return;
            }
            System.out.print(root.val+" ");
            preorder(root.left);
            preorder(root.right);
        }

    public static void main(String[] args) {
        Integer[] arr={1,null,2,3};
        TreeNode root=buildTreeByArr(arr,4,0);
        preorder(root);
    }
}

